package com.example.myapplication;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

import androidx.annotation.Nullable;

public class DataBaseHelper extends SQLiteOpenHelper {

    //自定义数据库名，版本
    static final private String SQLNAME = "MyTestSql";
    static final private int VERSION = 1;
    static final private String TABLENAME = "MyTableTest";
    private String tableSql = "create table "+TABLENAME+" (" +
            "_id int PRIMARY KEY AUTOINCREMENT," +
            "name TEXT NOT NULL," +
            "info TEXT NOT NULL);";
    private SQLiteDatabase database;
    private Context context;

    public DataBaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, SQLNAME, null, VERSION);
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
//        执行创建表
        database.execSQL(tableSql);
        Toast.makeText(context, "Table create successful", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        database.execSQL("DROP TABLE IF EXISTS " + TABLENAME );
        onCreate(database);
    }
}
